extends MoveStateResource


class_name SpeedStateResource


## 摩擦系数(减速用)
@export var frictional: float = 100
## 线性加速度(加速用)
@export var acceleration: float = 100
## 转向角速度
@export_range(0, 360, 1, "radians") var palstance: float = deg_to_rad(90)

## 最小速度
@export var min_speed: float = 0
## 最大速度
@export var max_speed: float = 200


func get_stand_position(ctrl: MoveController) -> Vector2:
    return ctrl.move_component.wrapper.global_position

func speed_up(ctrl: MoveController, delta: float) -> float:
    return move_toward(ctrl.move_component.speed, max_speed, acceleration * delta)

func speed_down(ctrl: MoveController, delta: float) -> float:
    return move_toward(ctrl.move_component.speed, min_speed, frictional * delta)

func turn_left(ctrl: MoveController, delta: float) -> Vector2:
    var increment = palstance * delta
    return ctrl.move_component.direction.rotated(-increment)

func turn_right(ctrl: MoveController, delta: float) -> Vector2:
    var increment = palstance * delta
    return ctrl.move_component.direction.rotated(increment)

# 朝指定的方向转向
func turn_to(ctrl: MoveController, direction: Vector2, delta: float) -> Vector2:
    # 没有角速度
    if palstance <= 0:
        return direction
    # 完全停止状态不需要转向
    if ctrl.move_component.is_stop():
        return direction
    var angle = direction.angle_to(ctrl.move_component.direction)
    var increment = palstance * delta
    if abs(angle) < increment:
        return direction
    var turn_angle = move_toward(0, angle, increment)
    return ctrl.move_component.direction.rotated(-turn_angle)
